from django.db import models

from oracle.base.basemodel import BaseModel, BaseMeta, DEFAULT_NAME, DEFAULT_DETAIL, DEFAULT_AVATAR


class OPermission(BaseModel):
    """
    权限表
    """
    title = models.CharField("标题", max_length=128, unique=True)
    url = models.CharField("含正则的URL", max_length=256)
    name = models.CharField("URL别名", max_length=128, unique=True)

    class Meta(BaseMeta):
        verbose_name = verbose_name_plural = "权限表"


class ORole(BaseModel):
    """
    角色表
    """
    title = models.CharField("角色名称", max_length=128, unique=True)
    permissions = models.ManyToManyField(OPermission, verbose_name="拥有的权限", blank=True)

    class Meta(BaseMeta):
        verbose_name = verbose_name_plural = "角色表"


class OUserInfo(BaseModel):
    """
    用户表
    """
    username = models.CharField("用户名", max_length=128, default=DEFAULT_NAME, unique=True)
    password = models.CharField("密码", max_length=128)
    avatar = models.CharField("头像", max_length=256, default=DEFAULT_AVATAR)
    detail = models.CharField("简介", max_length=256, default=DEFAULT_DETAIL)
    email = models.CharField("邮箱", max_length=128)
    roles = models.ManyToManyField(ORole, verbose_name="拥有的角色", blank=True)

    class Meta(BaseMeta):
        # 此类可以当作“父类”被其它Model类继承
        abstract = True
        verbose_name = verbose_name_plural = "用户表"

    @classmethod
    def show_field(cls):
        """
        展示用的字段
        :return:
        """
        show_fields = ["alias", "nickname", "name", "username"]
        for field in show_fields:
            if hasattr(cls, field):
                return field
        return "username"


class OMenu(BaseModel):
    """
    菜单表
    菜单情况说明，?表示可有可无 Y必须存在 N必须不存在
    |link_to_permission|parent|menu_inner|说明|
    |:-:|:-:|:-:|:-:|
    |N|N|N|顶级菜单(必须有子菜单)|
    |Y|N|N|顶级菜单(必须没有子菜单，强行设置也不会生效)|
    |N|Y|N|次级菜单(必须有子菜单)|
    |Y|Y|N|根菜单(必须没有子菜单，强行设置也不会生效)|
    |Y|N|Y|页面属性链接|
    |N|N|Y|错误，不存在|
    |Y|Y|Y|错误，不存在|
    |N|Y|Y|错误，不存在|
    """
    title = models.CharField("标题", max_length=128, unique=True)
    menu_icon = models.CharField("图标", max_length=256, null=True, blank=True)
    link_to_permission = models.OneToOneField(OPermission, models.SET_NULL, blank=True, verbose_name="关联权限", null=True)
    parent = models.ForeignKey('self', models.SET_NULL, 'menu_node_parent', verbose_name="上级菜单", blank=True,
                               null=True)
    menu_inner = models.ForeignKey('self', models.SET_NULL, 'menu_inner_parent', verbose_name="默认展开菜单", blank=True,
                                   null=True)

    class Meta(BaseMeta):
        verbose_name = verbose_name_plural = "菜单表"


class OIcon(BaseModel):
    """
    图标表
    """
    title = models.CharField("标题", max_length=128, unique=True)
    icon_class = models.CharField("图标", max_length=128, null=True, blank=True)
    category = models.ForeignKey('self', models.SET_NULL, verbose_name="上级菜单", blank=True, null=True)

    class Meta(BaseMeta):
        verbose_name = verbose_name_plural = "图标表"


# url类型
class UrlType(models.IntegerChoices):
    # 白名单， 任何人都可以访问
    white_list = 0, 'white list'
    # 自动发现时排除的url
    auto_discover_exclude = 1, 'auto discover exclude'
    # 登录后不需校验权限的URL
    access_for_all_login_users = 2, 'access for all login users'


class OSettingUrl(BaseModel):
    """
    设定url 列表
    """
    title = models.CharField("标题", max_length=128)
    url = models.CharField("url", max_length=256)
    type = models.IntegerField("url类型", default=0, choices=UrlType.choices)

    class Meta(BaseMeta):
        verbose_name = verbose_name_plural = "特殊url"
